package com.yuji.aacar.widgets.wheel;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.yuji.aacar.R;
import com.yuji.aacar.core.cores.utils.LogUtil;
import com.yuji.aacar.utils.TimeUtils;

import java.util.ArrayList;

import static java.lang.Integer.parseInt;


public class FromToTimePicker extends LinearLayout {
    public static final String TAG = "FromToTimePicker";
    private WheelView mWheelFromDays;
    private WheelView mWheelFromHour;
    private WheelView mWheelFromHourTime;
    private WheelView mWheelToHour;
    private WheelView mWheelToHourTime;

    private String nianyueri;

    private int day = 0;
    private String dayText = "今天";

    public static int DELAY_HOUR = 3;

    private int START_TIME = 7;
    private int END_TIME = 21;
    private boolean TODAY_OVER = false;

    private int mFromHour;
    private int mFromMin;
    private int mToHour;
    private int mToMinute;

    //    private boolean IS_INITIAL = true;
    //    private boolean IS_TIME_CHANGE = false;

    private OnResultListener onResultListener;


    //今天明天后天
    private WheelView.OnSelectListener mFromDayListener = new WheelView.OnSelectListener() {
        @Override
        public void endSelect(int hour, String text) {
            if (text.contentEquals("今天")) {
                day = 0;
                dayText = "今天";
                nianyueri = TimeUtils.getSystemWashDate(0);
                setDate();
                changeCurrentTime();
            } else if (text.contentEquals("明天")) {
                day = 1;
                dayText = "明天";
                nianyueri = TimeUtils.getSystemWashDate(1);
                setDate();
                changeCurrentTime();
            } else if (text.contentEquals("后天")) {
                day = 2;
                dayText = "后天";
                nianyueri = TimeUtils.getSystemWashDate(2);
                setDate();
                changeCurrentTime();
            }
        }

        @Override
        public void selecting(int id, String text) {
            //            mFromHour = id;
            LogUtil.e("当前时间选择了", mWheelFromHour.getSelectedText());
        }
    };

    private WheelView.OnSelectListener mFromHourListener = new WheelView.OnSelectListener() {
        @Override
        public void endSelect(int hour, String text) {
            //            if ()
            if (!text.contentEquals("--")){
                mFromHour = parseInt(text);
            }


            //            resetLastHour(Integer.parseInt(text));
            LogUtil.e("--OnSelectListener--mFromHourListener--", mFromHour);
            showNow();
            resetCurrentHourTime();
            showChange();
            //            resetLastHour();
            //            changeHour();
            changeCurrentTime();
        }

        @Override
        public void selecting(int id, String text) {
        }
    };


    private WheelView.OnSelectListener mFromHourTimeListener = new WheelView.OnSelectListener() {
        @Override
        public void endSelect(int hour, String text) {
            if (!text.contentEquals("--")){
                mFromMin = parseInt(text);
                LogUtil.e("--OnSelectListener--mFromHourTimeListener--", mFromMin);
                //            resetLastHour(Integer.parseInt(text));
                showNow();
                resetLastTime();
                changeTime();
                showChange();
            }

        }

        @Override
        public void selecting(int id, String text) {
        }
    };


    private WheelView.OnSelectListener mToHourListener = new WheelView.OnSelectListener() {
        @Override
        public void endSelect(int hour, String text) {
            if (!text.contentEquals("--")){
                mToHour = parseInt(text);
                LogUtil.e("--OnSelectListener--mToHourListener--", mToHour);
                showNow();
                resetLastTime();
                changeTime();
                showChange();
            }

        }

        @Override
        public void selecting(int hour, String text) {
        }
    };

    private WheelView.OnSelectListener mToHourTimeListener = new WheelView.OnSelectListener() {
        @Override
        public void endSelect(int hour, String text) {
            if (!text.contentEquals("--")){
                mToMinute = parseInt(text);
                LogUtil.e("--OnSelectListener--mToHourTimeListener--", mToMinute);
                showNow();
            }

        }

        @Override
        public void selecting(int hour, String text) {
        }
    };

    private void showNow() {
        LogUtil.e("--OnSelectListener--showNow--", mFromHour + ":" + mFromMin + "--" + mToHour + ":" + mToMinute);
    }

    private void showChange() {
        LogUtil.e("--OnSelectListener--showChange--", mFromHour + ":" + mFromMin + "--" + mToHour + ":" + mToMinute);
    }


    private Activity mContext;

    public FromToTimePicker(Context context) {
        this(context, null);
    }

    public FromToTimePicker(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setWheelViewItemNumber(int number) {
        mWheelFromHour.setItemNumber(number);
        mWheelToHour.setItemNumber(number);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mContext = (Activity) getContext();
        LayoutInflater.from(mContext).inflate(R.layout.layout_time_picker, this);
        mWheelFromDays = (WheelView) findViewById(R.id.from_day);
        mWheelFromHour = (WheelView) findViewById(R.id.from_hour);
        mWheelFromHourTime = (WheelView) findViewById(R.id.from_hour_time);
        mWheelToHour = (WheelView) findViewById(R.id.to_hour);
        mWheelToHourTime = (WheelView) findViewById(R.id.to_hour_time);

        mWheelFromDays.setOnSelectListener(mFromDayListener);
        mWheelFromHour.setOnSelectListener(mFromHourListener);
        mWheelFromHourTime.setOnSelectListener(mFromHourTimeListener);
        mWheelToHour.setOnSelectListener(mToHourListener);
        mWheelToHourTime.setOnSelectListener(mToHourTimeListener);
        setDate(); //1 后天 //2今天 //3明天
    }


    private ArrayList<String> getHourData() {
        ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < 24; i++) {
            list.add(i + "");
        }
        return list;
    }

    private ArrayList<String> getMinuteData(int max) {
        ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i <= max; i++) {
            list.add(i + "");
        }
        return list;
    }

    public interface OnResultListener {
        void onConfirm(int fromHour, int fromMinute, int toHour, int toMinute);

        void onCancel();
    }

    public void setDate() {

        nianyueri = TimeUtils.getSystemWashDate(day);

        ArrayList<String> days = new ArrayList<>();
        days.add("后天");
        days.add("今天");
        days.add("明天");
        mWheelFromDays.setData(days);
        resetCurrentHour();

    }

    /**
     * 更改 预约 明天的时间
     */
    private void resetCurrentHour() {
        ArrayList<String> hours = new ArrayList<>();
        int mFromHourTemp = 0;
        String[] hourAndTimeStr;

        if (day == 0) {//今天

            String hourAndTime = TimeUtils.getSystemWashTime();//系统当前当前 2：10
            hourAndTimeStr = hourAndTime.split(":");
            String systemTimeHour = hourAndTimeStr[0]; //2
            String systemTimeMinu = hourAndTimeStr[1]; //10
            String convertTime = TimeUtils.getWashConvertDayTime(systemTimeHour, systemTimeMinu);//2:40
            hourAndTimeStr = convertTime.split(":");
            int systemTime = parseInt(hourAndTimeStr[0]);// 2 或者 3

            if (systemTime < START_TIME) { //没到工作时间
                mFromHourTemp = START_TIME;
            } else if (systemTime >= END_TIME) {//超过21点工作时间
                mFromHourTemp = END_TIME;
                TODAY_OVER = true;
            } else {//工作时间内
                mFromHourTemp = systemTime;
            }
        } else {
            mFromHourTemp = START_TIME;
        }


        if (day ==0 && TODAY_OVER) {//当天到21点后就不能预约了
            hours.add("--");
        } else {
            for (int i = mFromHourTemp; i < 22; i++) {
                hours.add(i + "");
            }
            mFromHour = mFromHourTemp;
            //            LogUtil.e("生成时间---hours---", hours);
        }

        //        LogUtil.e("界面显示的--小时---和工作时间匹配后的时间--", mFromHourTemp);
        //        LogUtil.e("当前时间选择了---和工作时间匹配后的时间--", mFromHour + ":" + mFromMin);

        mWheelFromHour.refreshData(hours);
        resetCurrentHourTime();
    }


    /**
     * 当天起始时间 点
     */
    private void resetCurrentHourTime() {
        ArrayList<String> hoursTimes = new ArrayList<>();
        int mFromMinTemp = 0;

        String[] hourAndTimeStr = new String[2];

        if (day ==0 &&TODAY_OVER) {
            hoursTimes.add("--");
        } else {

            String hourAndTime = TimeUtils.getSystemWashTime();//系统当前当前 2：10
            hourAndTimeStr = hourAndTime.split(":");
            String systemTimeHour = hourAndTimeStr[0]; //2
            String systemTimeMinu = hourAndTimeStr[1]; //10
            String convertTime = TimeUtils.getWashConvertDayTime(systemTimeHour, systemTimeMinu);//2:40
            hourAndTimeStr = convertTime.split(":");
            int systemTime = parseInt(hourAndTimeStr[0]);// 2 或者 3

            if (mFromHour == END_TIME) {
                mFromMin = 0;
                hoursTimes.add("00");
            } else {
                if (day == 0) {//今天

                    if (systemTime < START_TIME) { //没到工作时间
                        mFromMin = 0;
                    } else if (systemTime == END_TIME) {//超过21点工作时间
                        mFromMin = 0;
                    } else {//工作时间内
                        if (mFromHour <= systemTime) {
                            mFromMin = parseInt(hourAndTimeStr[1]); //40
                            mFromMinTemp = mFromMin / 10;
                            //                        IS_TIME_CHANGE = true;
                        } else {
                            mFromMin = 0;
                        }
                    }
                } else {
                    mFromMin = 0;
                }

                for (int i = mFromMinTemp; i < 6; i++) {
                    hoursTimes.add(i + "0");
                }
            }
        }
        mWheelFromHourTime.refreshData(hoursTimes);
        //        mWheelFromHourTime.setDefault(0);
        resetLastHour();


    }


    /**
     * 更改 明天的 小时 时间
     */
    private void resetLastHour() {
        ArrayList<String> toHour = new ArrayList<>();

        int fromHour = 0;
        if (day ==0 && TODAY_OVER) {
            toHour.add("--");
        } else {
            //            if (IS_INITIAL) {
            //                //            String[] convertTime = TimeUtils.getTwoHoursLaterTime(nianyueri + " "+mWheelFromHour.getSelectedText()+":"+mWheelFromHourTime.getSelectedText());
            //                //            int convertHour = Integer.parseInt(convertTime[1]);
            //                fromHour = Integer.parseInt(mWheelFromHour.getItemText(0));
            //                //            fromHour = convertHour;
            //            } else {
            //                fromHour = mFromHour;
            //            }
            fromHour = mFromHour;
            mToHour = fromHour + DELAY_HOUR;
            if (mToHour > 23) {
                mToHour = 23;
            }
            for (int i = mToHour; i < 24; i++) {
                toHour.add(i + "");
            }
        }


        mWheelToHour.refreshData(toHour);
        resetLastTime();
    }

    /**
     * 更改 明天的 小时 时间
     */
    private void resetLastTime() {

        ArrayList<String> toHourTime = new ArrayList<>();

        int fromTime = 0;

        showNow();
        if (day ==0 && TODAY_OVER) {
            toHourTime.add("--");
        } else {

            LogUtil.e("--传入分钟--", +mFromMin);

            if (mFromHour == END_TIME) {
                fromTime = 5;
                mToMinute = 50;
            } else {
                if (mToHour - mFromHour <= DELAY_HOUR) {
                    fromTime = mFromMin / 10;
                } else if (mToHour > mFromHour + DELAY_HOUR) {
                    fromTime = 0;
                }
                mToMinute = fromTime * 10;
            }


            for (int i = fromTime; i < 6; i++) {
                toHourTime.add(i + "0");
            }
        }
        showChange();

        mWheelToHourTime.refreshData(toHourTime);
    }


    public void setCurrentDate(String from, String to) {
        mWheelFromDays.setDefault(1);
        mWheelFromHour.setDefault(0);
        mWheelFromHourTime.setDefault(0);
        mWheelToHour.setDefault(0);
        mWheelToHourTime.setDefault(0);
    }

    public String[] getFinalChooseDate() {
        String[] back = new String[4];

        nianyueri = TimeUtils.getSystemWashDate(day);

        String a = mWheelFromHour.getSelectedText();
        String b = mWheelFromHourTime.getSelectedText();
        String c = mWheelToHour.getSelectedText();
        String d = mWheelToHourTime.getSelectedText();
        String finalDate = nianyueri + " " + a + ":" + b + "~" + c + ":" + d;

        back[0] = dayText;
        back[1] = nianyueri;
        back[2] = a + ":" + b;
        back[3] = c + ":" + d;
        LogUtil.e("finaltime ", back);
        return back;
    }

    public String getFinalShowDate() {
        nianyueri = TimeUtils.getSystemWashDate(day);

        String a = mWheelFromHour.getSelectedText();
        String b = mWheelFromHourTime.getSelectedText();
        String c = mWheelToHour.getSelectedText();
        String d = mWheelToHourTime.getSelectedText();
        String finalDate = dayText + " " + a + ":" + b + "~" + c + ":" + d;
        LogUtil.e("finaltime ", finalDate);
        return finalDate;
    }

    public void changeCurrentTime() {
        //        mWheelFromHour.setDefault(0);
        mWheelFromHourTime.setDefault(0);
        mWheelToHour.setDefault(0);
        mWheelToHourTime.setDefault(0);
    }

    public void changeHour() {
        mWheelToHour.setDefault(0);
    }

    public void changeTime() {
        mWheelToHourTime.setDefault(0);
    }

    public void setOnResultListener(OnResultListener onResultListener) {
        this.onResultListener = onResultListener;
    }


    public String getImmediateReserveFromTime() {
        ArrayList<String> hours = new ArrayList<>();
        ArrayList<String> hoursTimes = new ArrayList<>();


        int mFromHourTemp = 0;
        String[] hourAndTimeStr;

        String hourAndTime = TimeUtils.getSystemWashTime();
        hourAndTimeStr = hourAndTime.split(":");
        hourAndTimeStr = TimeUtils.getWashConvertDayTime(hourAndTimeStr[0], hourAndTimeStr[1]).split(":");
        int systemTime = parseInt(hourAndTimeStr[0]);

        if (systemTime < START_TIME) { //没到工作时间
            mFromHourTemp = START_TIME;
        } else if (systemTime == END_TIME) {//超过21点工作时间
            mFromHourTemp = END_TIME;
        } else {//工作时间内
            mFromHourTemp = systemTime;
        }


        if (mFromHourTemp > END_TIME) {//当天到21点后就不能预约了
            TODAY_OVER = true;
            hours.add("--");
        } else {
            TODAY_OVER = false;
            for (int i = mFromHourTemp; i < 22; i++) {
                hours.add(i + "");
            }
            LogUtil.e("生成时间---hours---", hours);
        }


        int mFromMinTemp = 0;
        if (systemTime < START_TIME) { //没到工作时间
        } else if (systemTime == END_TIME) {//超过21点工作时间
        } else {//工作时间内
            if (mFromHour <= systemTime) {
                mFromMinTemp = parseInt(hourAndTimeStr[1]) / 10;
            } else {

            }
        }

        for (int i = mFromMinTemp; i < 6; i++) {
            hoursTimes.add(i + "0");
        }

        if (hours.get(0).contentEquals("--")) {
            nianyueri = TimeUtils.getSystemWashDate(1);
            return "7:00";
        } else {
            nianyueri = TimeUtils.getSystemWashDate(0);
            return hours.get(0) + ":" + hoursTimes.get(0);
        }
    }


    public String getImmediateReserveToTime(String time) {
        String[] str = time.split(":");
        String backTime = Integer.parseInt(str[0]) + DELAY_HOUR + "";
        return backTime + ":" + str[1];
    }

    public String getImmediateReserveNYR() {
        return nianyueri;
    }

    public String getDayText() {
        return dayText;
    }
}
